package com.hxd.otc.module.my.activity;

import android.content.Intent;
import android.net.Uri;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.order.bean.AllOrderBean;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.other.rong.OrderInfoFloatingMessage;
import com.hxd.otc.widget.TitleBar;

import butterknife.BindView;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;

/**
 * Created by lzy on 2018/8/13 0013.
 * description ${聊天Activity}
 */
public class ChatActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar mTitleBar;

    public static final String EXTRA_ORDER_INFO = "order_info";
    private String mTargetId;

    protected void init() {
        //重写该方法是因为BaseActivity的initWindow导致软键盘无法抬起输入框，临时解决方法
        mPresenter = createPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_chat;
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        Uri data = intent.getData();
        if (data == null) return;
        mTargetId = data.getQueryParameter("targetId");
        mTitleBar.setTitleText(data.getQueryParameter("title"));

        sendFloatingMessage();
    }

    private void sendFloatingMessage() {
        AllOrderBean.DataBean orderBean = (AllOrderBean.DataBean) getIntent().getSerializableExtra(EXTRA_ORDER_INFO);
        if (orderBean == null) return;
        OrderInfoFloatingMessage orderFloatMessage = new OrderInfoFloatingMessage(orderBean);
        RongIM.getInstance().insertOutgoingMessage(Conversation.ConversationType.PRIVATE, mTargetId, Message.SentStatus.SENT, orderFloatMessage, new RongIMClient.ResultCallback<Message>() {
            @Override
            public void onSuccess(Message message) {
                if (message.getMessageId() == -1) return;
                RongIMClient.getInstance().deleteMessages(new int[]{message.getMessageId()}, null);
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {
            }
        });
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
